grep commands

This is powerful command while searching an string/pattern with regular expression on the file

To display searched lines with case sensitive

Display matched line only 

grep –i “hello” test.txt


To display the searched lines contains with between words.

grep “hello.*empty” test.txt


To display the searched lines contains matched word with case sensitive.

grep -iw “hello” test.txt


To display lines after/before the lines contains matched words, -A,-B,-C

grep -A 2 –I “hello” test.txt

grep –B 2 –I “hello” test.txt

grep –C 2 –I “hello” test.txt


To search matched word in the current directory and its sub directory in recursively.

grep -r “ramesh” test.txt


To search invert match of word in the file

grep –v “hello” test.txt


Invert math with multiple search

grep –v –e “hello” –e “word” test.txt


To find lines count contains the matched word

grep -c “je” test.txt


To find the lines count not contains the matched word

grep –v –c “hi” test.txt


To find the list of files contains the matched word

grep –l “hello” demo_*


To find the matched string of pattern

grep –o “this.*line” test.txt


To show position of matched string in the file

grep –o –p “3” test.txt


To show line number of line contains the matched string

grep –n “hello” test.txt


To find number of occurrences’ of the given word in the file

grep –ow “hello” test.txt | wc –l

No comments:

Post a Comment